import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;


public class AppContent {
	
	public int currNum = 0;
	private String tmpCmd = new String("reg");
	
	public AppContent () {
		
	}
	
	public AppContent (String ver) {
		PlugInReg(ver);
		addKey_Value("Config", "|maintainReaded=1|");
	}
	
	public void PlugInReg(String ver) {
		tmpCmd = "reg add HKLM\\SOFTWARE\\CII_PANEL";
		try {
		      Runtime.getRuntime().exec(tmpCmd);
		      Runtime.getRuntime().exec("reg add HKLM\\SOFTWARE\\CII_PANEL /v \"ver\" /t REG_SZ " +
		      		"/d "+ver);
		} catch (Exception e) {
			System.out.println("Error adding content:\n"+e);
		}
		
	}
	
	public void addLastOpened (String path) {
		tmpCmd = "reg add HKLM\\SOFTWARE\\CII_PANEL /v \"opened\""+currNum+" /t REG_SZ /d \""+ path+"\"";
		try {
		      Runtime.getRuntime().exec(tmpCmd);
		} catch (Exception e) {
			System.out.println("Error adding path:\n"+e);
		}
		currNum ++;
	}
	
	public void addKey_Value(String Keyname, String Value){
		tmpCmd = "reg add HKLM\\SOFTWARE\\CII_PANEL /v ";
		try {
		      Runtime.getRuntime().exec(tmpCmd+Keyname+" /t REG_SZ /d "+Value);
		} catch (Exception e) {
			System.out.println("Error adding key and value:\n"+e);
		}
		
	}
	
	public String getVlaue(String ofKey) {
		tmpCmd = "reg query HKLM\\SOFTWARE\\CII_PANEL\\ /v "+ofKey; 
		try {
		      Process process = Runtime.getRuntime().exec(tmpCmd);
		      StreamReader reader = new StreamReader(process.getInputStream());

		      reader.start();
		      process.waitFor();
		      reader.join();

		      String result = reader.getResult();
		      
		      
		      for (int i=0;i<10;i++) System.out.println((char)process.getErrorStream().read());
		      int p = result.indexOf("REG_SZ");

		      if (p == -1)
		      return result.substring(p + "REG_SZ".length()).trim();
		    }
		    catch (Exception e) {
		    	System.out.println("An error attempting access registry\n"+e);
		    	return null;
		    }
		return "";
	}
	
	public String getLastOpened (int index) {
		String prepareCmd = new String("");
		//if (index>currNum) return "Out of range";
		prepareCmd = "reg query HKLM\\SOFTWARE\\CII_PANEL\\ /v opened"+index;
		try {
		      Process process = Runtime.getRuntime().exec(prepareCmd);
		      StreamReader reader = new StreamReader(process.getInputStream());

		      reader.start();
		      process.waitFor();
		      reader.join();

		      String result = reader.getResult();
		      
		      
		      for (int i=0;i<10;i++) System.out.println((char)process.getErrorStream().read());
		      int p = result.indexOf("REG_SZ");

		      if (p == -1)
		      return result.substring(p + "REG_SZ".length()).trim();
		    }
		    catch (Exception e) {
		    	System.out.println("An error attempting access registry\n"+e);
		    	return null;
		    }
		    return "-1";
	}
	
	public int getCurrNum () {
		try {
		Process process = Runtime.getRuntime().exec("reg query HKLM\\SOFTWARE\\CII_PANEL\\ /v opened"+0);
		int i = 0;
		byte[] tmp = new byte[1];
		while (process.getErrorStream().read(tmp)==-1) {
			i++;
			process = Runtime.getRuntime().exec("reg query HKLM\\SOFTWARE\\CII_PANEL\\ /v opened"+i);
			
		}
		return i;
		} catch (Exception c ) {System.out.println(c); }
		return 0;
	}
	
	
	static class StreamReader extends Thread {
	    private InputStream is;
	    private StringWriter sw;

	    StreamReader(InputStream is) {
	      this.is = is;
	      sw = new StringWriter();
	    }

	    public void run() {
	      try {
	        int c;
	        while ((c = is.read()) != -1)
	          sw.write(c);
	        }
	        catch (IOException e) { ; }
	      }

	    String getResult() {
	      return sw.toString();
	    }
	  }
	
}
